<template>
	<view class="mask" @tap="cancel" @touchmove.stop>
		<view class="mask-content" @tap.stop>
			<text class="new-iconfont icon-close close-btn" @tap="cancel"></text>
			<view class="title">
				提交发货
			</view>

			<view class="block">
				<view class="item-title">已选订单:</view>
				<view class="item-value">{{selectedId.length}}个</view>
			</view>
			
			<view class="block" v-if="isMultiDeliverType">
				<view class="item-title">发货方式:</view>
				<view class="deliver-type-list">
					<view class="item" :class="{actived: deliverType === 'express'}"  @tap="deliverType = 'express'">快递</view>
					<view class="item" :class="{actived: deliverType === 'custom_gateway'}" @tap="deliverType = 'custom_gateway'">商家物流</view>
				</view>
			</view>
			
			<view class="block">
				<view class="item-title">运费:</view>
				<view class="item-value" v-if="deliverType === 'express'">￥{{$tool.formatPrice(carriage)}}</view>
				<view class="item-value" v-else-if="deliverType === 'custom_gateway'">￥0</view>
			</view>

			<view class="block">
				<view class="item-title">可用优惠券:</view>
				<view class="arrow-right bold" @tap="isCouponPopup = true">
					<text class="meta" v-if="order.coupon_discount"> - ¥{{$tool.formatPrice(order.coupon_discount)}}</text>
					<template v-else>
						<text class="meta" style="color: #FF1A1A;"
							v-if="usableCoupons.length">{{usableCoupons.length}}张可用</text>
						<text class="meta gray-text" v-else>暂无可用优惠券</text>
					</template>
					<text class="new-iconfont icon-arrow-right"></text>
				</view>
			</view>

			<!-- <view class="sku-list">
				<view class="item" @tap="selectSku(item)" :class="{actived: item.id === skuId}" v-for="(item, index) in skuList">{{item.title}}</view>
			</view> -->
			<!-- <view class="item-title">收货地址:</view> -->
			<view style="height: 30rpx;"></view>
			<SelectAddress class="address-c" v-model="address"></SelectAddress>
			<!-- <view class="redpack">
				<view class="body">
					本单可用红包
				</view>
				<view class="arrow-right bold">
					<text class="meta red"> ¥{{redpack | priceToFixed}}</text>
					<switch v-if="redpack" class="redpack-switch" :checked="isUseRedpack" @change="switchChange" />
				</view>
			</view> -->
			<view class="total">
				总计：
				<PriceDisplay :info="order" prefix="pay_" v-if="deliverType === 'express'"></PriceDisplay>
				<text v-else>￥0</text>
			</view>
			
			<view class="tips">{{deliverTips}}</view>
			<view class="button" @click="submit">
				<text>确认提交发货</text>
			</view>
		</view>

		<UsableCouponPopup v-if="isCouponPopup" :unusableCoupons="unusableCoupons" :usableCoupons="usableCoupons"
			@change="couponChange" @close="isCouponPopup = false">
		</UsableCouponPopup>

	</view>
</template>

<script>
	import payment from "@/utils/payment.js"
	export default {
		components: {},
		data() {
			return {
				address: {},
				order: {},
				carriage: 0,
				unusableCoupons: [],
				usableCoupons: [],
				isCouponPopup: false,
				currentCoupon: {},
				deliverType: '',
				isMultiDeliverType: false
			}
		},
		props: {
			selectedId: {
				type: Array
			}
		},
		computed: {
			orderConfig () {
				return  this.$store.getters.setting.order  || {}
			},
			deliverTips () {
				return this.orderConfig.deliver_tips || '商品一经寄出，非质量问题不支持退换'
			}
		},
		watch: {
			address() {
				this.initOrder()
			}
		},
		onLoad(e) {

		},
		created() {
			this.initOrder()
			// this.$http('/package/deliver-order/config')
		},
		methods: {
			st_loadData(){
				let st = true;
				if(st == 1){
					console.info(1)
				}
			},
			st_list(){
				let status = true;
				if(status){
					status = false;
					console.info(222)
				}
			},
			st_pay(){
				let s = 1;
				if(s == 1){
			
				}else if(s == 2){
				
				}
			},
			couponChange (e) {
				if (e.id === this.currentCoupon.id) {
					// 再次点击取消使用优惠券  // 暂时关闭此功能
					// this.currentCoupon = {}
				} else {
					this.currentCoupon = e
					this.initOrder()
				}
			},
			initOrder() {
				// if (!this.address.id) {
				// 	return false
				// }
				this.$http('/package/deliver-order/preview', 'POST', {
					ids: this.selectedId,
					address_id: this.address.id,
					coupon_id: this.currentCoupon.id,
				}).then(res => {
					this.carriage = res.data.info.carriage
					this.order = res.data.info,
					this.unusableCoupons = res.data.info.coupons.unusable
					this.usableCoupons = res.data.info.coupons.usable
					this.address = res.data.info.address
					
					// 有开启商家物流
					if (res.data.is_custom_gateway_enabled) {
						this.deliverType = this.deliverType || 'express'
						this.isMultiDeliverType = true
					}
					else {
						this.deliverType = 'express'
						this.isMultiDeliverType = false
					}
				})
			},
			cancel() {
				this.$emit('cancel')
			},
			submit() {

				if (!this.address.id) {
					uni.showModal({
						title: '请选择收货地址',
					})
					return false
				}

				uni.showLoading({
					title: '提交中',
					icon: 'none'
				})
				
				if (this.deliverType === 'express') {
					this.expressDeliver()
				}
				else if (this.deliverType === 'custom_gateway') {
					this.customGatewayDeliver()
				}
			},
			// 物流发货
			expressDeliver() {
				this.$http('/package/deliver-order/confirm', 'POST', {
					ids: this.selectedId,
					address_id: this.address.id,
					coupon_id: this.currentCoupon.id,
				}).then(res => {
					uni.hideLoading()
					let info = res.data
					if (info.is_need_pay) {
						payment.pay({
							pay_config: info.pay_config,
							success: () => {
								// uni.showToast({
								// 	title: "支付成功",
								// 	icon: "none"
								// })
								this.$emit('success')
							},
							fail: () => {
								uni.showToast({
									title: "支付失败",
									icon: "none"
								})
				
								// 关闭订单
								// this.$http(`/orders/${info.order.uuid}`, 'PUT', {
								// 	type: 'close_and_delete'
								// })
							}
						})
					} else {
						this.$emit('success', {
							deliver_type: 'express'
						})
					}
				})
			},
			// 商家物流发货
			customGatewayDeliver() {
				this.$http(`/market/resale/batch/confirm`, 'post', {
					ids: this.selectedId,
					mode: 'custom_gateway'
				}).then(res => {
					uni.hideLoading()
					this.$emit('success', {
						deliver_type: 'custom_gateway'
					})
				})
			}
 		},
		onPageScroll(e) {}
	}
</script>

<style lang="scss" scoped>
	.st_main{
		background-color: transparent;
		font-size: 28rpx;
		font-weight: bold;
		display: flex;
		flex-direction: row;
	}
	.st_list{
		background-color: transparent;
		display: flex;
		font-size: 28rpx;
	}
	.small-text {
		// font-size: 80%;
	}

	.mask-content {
		position: absolute;
		bottom: 0;
		width: 100%;
		// height: 860rpx;
		background-color: #EDEDED;
		background-color: white;
		border-radius: 10rpx 10rpx 0 0;
		// display: flex;
		// flex-direction: column;
		// align-items: center;
		// animation: show 0.8s;
		padding: 30rpx 30rpx;
		box-sizing: border-box;

		.close-btn {
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}

		.title {
			text-align: center;
			font-size: 36rpx;
			font-weight: 500;
			margin-bottom: 30rpx;
		}

		.item-title {
			margin: 0rpx 0rpx 0rpx 0rpx;
			font-size: 28rpx;
			// color: gray;
		}

		.item-value {
			text-align: right;
			font-weight: 500;
		}

		.block {
			display: flex;
			align-items: center;
			padding: 16rpx 0rpx;
			// margin-top: 20rpx;
			
			.deliver-type-list {
				display: flex;
				.item {
					font-weight: 500;
					border-radius: 40rpx;
					border: 2rpx solid #e1e1e1;
					// background: #e1e1e1;
					padding: 4rpx 20rpx;
					margin-left: 20rpx;
					font-size: 24rpx;
					
					&.actived {
						background: #F27233;
						border: 2rpx solid rgba(242, 114, 51, 1);
						color: white;
					}
				}
			}

			.item-title {
				// margin: 0rpx 0rpx 0rpx 0rpx;
				// font-size: 28rpx;
				// color: gray;
				flex-grow: 1;
				font-weight: 500;
			}

			.price-c {}

			.redpack-switch {
				margin-left: 10rpx;
				position: relative;
				left: 10rpx;
				transform: scale(0.8);
			}
		}
		
		.address-c {
			margin-top: 30rpx;
		}

		.sku-list {
			display: flex;
			flex-wrap: wrap;

			.item {
				padding: 0 26rpx;
				height: 48rpx;
				line-height: 48rpx;
				font-size: 24rpx;
				margin-right: 16rpx;
				margin-bottom: 16rpx;
				border-radius: 40rpx;
				border: 2rpx solid #EAEBED;
				text-align: center;
				min-width: 70rpx;

				&.actived {
					color: white;
					background: rgba(242, 114, 51, 1);
					border: 2rpx solid rgba(242, 114, 51, 1);
				}

				&:last-child {
					margin-right: 0rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			width: 100%;
			box-sizing: border-box;
			padding: 0 16rpx;
			text-align: right;
			color: red;
			font-size: 28rpx;
		}
		
		.tips {
			text-align: center;
			color: red;
			font-size: 24rpx;
			margin-top: 60rpx;
		}

		.button {

			padding: 0;
			margin: 20rpx auto 0rpx auto;
			width: 100%;
			border-radius: 50rpx;
			height: 86rpx;
			line-height: 86rpx;
			text-align: center;
			font-size: 30rpx;
			font-weight: bold;
			background-color: #ffcc00;
			box-shadow: 0 0 20rpx rgba(255, 220, 0, 0.6);


			&:after {
				border: none;
			}
		}
	}
</style>
